﻿using Liju.Common.WebReference;
using Liju.Common.WebReference1;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace Liju.Common
{
    /// <summary>
    /// BYD MES
    /// </summary>
    public static class MES
    {
        /// <summary>
        /// 是否用EMS接口
        /// </summary>
        public static bool IsUseMES = false;

        /// <summary>
        /// MES服务器IP
        /// </summary>
        public static string MesIP = "10.9.29.198";

        /// <summary>
        /// MES服务端口
        /// </summary>
        public static string MesPort = "2881";

        /// <summary>
        /// 登录员工工号 camstaradmin
        /// </summary>
        public static string UserGongHao = "camstaradmin";

        /// <summary>
        /// 登录员工账号密码 Cam1star
        /// </summary>
        public static string UsernPW = "Cam1star";

        /// <summary>
        /// MES登录验证状态 true 通过 false 未过
        /// </summary>
        public static bool MesCheckPass = false;

        /// <summary>
        /// 产线
        /// </summary>
        public static string Line = "D01-GYXS-L17";

        /// <summary>
        /// 工站信息
        /// </summary>
        public static string WorkStation = "GYXS-DGL17-NLS01";

        /// <summary>
        /// 资源号
        /// </summary>
        public static string ZiYuanNo = "DGG2024727";

        /// <summary>
        /// 连接状态
        /// </summary>
        public static bool conState;

        /// <summary>
        /// 检查连接状态 未连则连接
        /// </summary>
        private static void CheckConnect()
        {
        }

        /// <summary>
        /// 1. 员工登录验证 无权限角色 默认操作员权限 特殊权限用本软件的账号角色
        /// </summary>
        public static MESRetrun Login()
        {
            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            // { "InterfaceJudgment":true,"ErrInfo":"登录成功","NJZ_data_list":{ },
            // "NJZ_DataTable":[],"NJZ_DatalistObject":[]}
            DateTime dtFrom = DateTime.Now;
            try
            {
                TightenTheShaft tightenTheShaft = new TightenTheShaft();
                string strReturn = tightenTheShaft.NJZ_LoginValidate(strMESPama, false);
                UnifiedObj.WriteLog(2, " 耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 登录 参数:" + "\r\n" + strMESPama + "\r\n返回:\r\n" + strReturn + "\r\n");

                if (strReturn.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                else
                {
                    return JsonHelper.JsonToObject<MESRetrun>(strReturn);
                }
            }
            catch (Exception ex)
            {
                UnifiedObj.WriteLog(2, " 耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 登录 参数:" + "\r\n" + strMESPama + "\r\n异常:\r\n" + ex.Message + "\r\n");
                if (ex.Message.Contains("无法连接到远程服务器") || ex.Message.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常:" + ex.Message + "\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
            }
            //    "\"PW\":\"Cam1star2\""
            // { "InterfaceJudgment":false,"ErrInfo":"验证登录信息失败,错误信息为：Login Failed - invalid user name or password",
            // "NJZ_data_list":{ },"NJZ_DataTable":[],"NJZ_DatalistObject":[]}
        }

        /// <summary>
        /// 2. 验证工站资源 
        /// </summary>
        public static MESRetrun CheckWorkStation()
        {
            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //WorkStation = "GYXS-DGL17-NLS012";
            //ZiYuanNo = "DGG2024727";
            // {"InterfaceJudgment":true,"ErrInfo":"获取设备信息成功：",
            // "NJZ_data_list":{"DevType":{"Name":"设备信息","Value":"电动拧紧轴03"}},
            // "NJZ_DataTable":[],"NJZ_DatalistObject":[]}
            DateTime dtFrom = DateTime.Now;
            try
            {
                TightenTheShaft tightenTheShaft = new TightenTheShaft();
                string strReturn = tightenTheShaft.NJZ_ResourceValidate(strMESPama, WorkStation, ZiYuanNo);
                UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 验证工站资源 工站信息: " + WorkStation + 
                    " 资源号: " + ZiYuanNo + "\r\n返回:\r\n" + strReturn + "\r\n");

                if (strReturn.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                else
                {
                    return JsonHelper.JsonToObject<MESRetrun>(strReturn);
                }
            }
            catch (Exception ex)
            {
                UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 验证工站资源 工站信息: " + WorkStation + 
                    " 资源号: " + ZiYuanNo + "\r\n异常:\r\n" + ex.Message + "\r\n");
                if (ex.Message.Contains("无法连接到远程服务器") || ex.Message.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常:" + ex.Message + "\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
            }
            //WorkStation = "GYXS-DGL17-NLS012";

            //{ "InterfaceJudgment":false,"ErrInfo":"获取设备信息失败培训资质验证失败!",
            //    "NJZ_data_list":{ },"NJZ_DataTable":[],"NJZ_DatalistObject":[]    }
        }

        /// <summary>
        /// 3. 根据产品SN获取当前工序的产品信息 true可作业 false不可作业
        /// </summary>
        /// <param name="ProductSN">产品SN</param>
        public static MESRetrun GetWorkStationProduct(string ProductSN)
        {
            UnifiedObj.WriteLog(2, "MES获取SN信息 开始\r\n");

            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //ProductSN = "01046174XR72600042",
            //WorkStation = "GYXS-DGL17-NLS01";
            //{ "InterfaceJudgment":true,"ErrInfo":"资源校验通过:",
            //"NJZ_data_list":{ "OrderNumber":{ "Name":"订单号","Value":"003118247347"},"ProductionLine":{ "Name":"生产线","Value":"D01-GYXS-L17"},"WorkingProcedure":{ "Name":"当前工序","Value":"GYXS-NLS01"},"MaterialNo":{ "Name":"物料号","Value":"13350560-00"},"MaterialRev":{ "Name":"物料版本","Value":"C/3"},"MaterialName":{ "Name":"物料名","Value":"HCHN-2106010集成式充电插座总成"},"Workflow":{ "Name":"工作流","Value":"13350560-00"} },
            //"NJZ_DataTable":[],"NJZ_DatalistObject":[]}
            DateTime dtFrom = DateTime.Now;
            try
            {
                TightenTheShaft tightenTheShaft = new TightenTheShaft();
                string strReturn = tightenTheShaft.NJZ_SN_Validate(strMESPama, ProductSN, WorkStation);

                UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now-dtFrom).TotalMilliseconds + " 获取当前工序的产品信息 SN:" + ProductSN + 
                    " 工站信息: " + WorkStation + "\r\n返回:\r\n" + strReturn + "\r\n");

                //20241123 mes服务器崩了返回 {"InterfaceJudgment":false,"ErrInfo":"获取批次信息失败!连接服务器异常","NJZ_data_list":{},"NJZ_DataTable":[],"NJZ_DatalistObject":[]}
                if (strReturn.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                else
                {
                    return JsonHelper.JsonToObject<MESRetrun>(strReturn);
                }
            }
            catch (Exception ex)
            {
                UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 获取当前工序的产品信息 SN:" + ProductSN +
                    " 工站信息: " + WorkStation + "\r\n异常:\r\n" + ex.Message + "\r\n");
                if (ex.Message.Contains("无法连接到远程服务器") || ex.Message.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常:" + ex.Message + "\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
            }
            //01046174XR72600042 ok过站后在查
            //{ "InterfaceJudgment":false,"ErrInfo":"[拧螺丝02工位]工序未作业，请到[GYXS-DGL17-NLS02_拧螺丝02]进行作业！",
            //"NJZ_data_list":{ "OrderNumber":{ "Name":"订单号","Value":"003118247347"},"ProductionLine":{ "Name":"生产线","Value":"D01-GYXS-L17"},"WorkingProcedure":{ "Name":"当前工序","Value":"GYXS-NLS02"},"MaterialNo":{ "Name":"物料号","Value":"13350560-00"},"MaterialRev":{ "Name":"物料版本","Value":"C/3"},"MaterialName":{ "Name":"物料名","Value":"HCHN-2106010集成式充电插座总成"} },
            //"NJZ_DataTable":[],"NJZ_DatalistObject":[]}
        }

        /// <summary>
        /// 4. 打螺丝PASS 
        /// </summary>
        /// <param name="ProductSN">产品SN</param>
        /// <param name="listNiuLi">各螺丝扭力数组</param>
        /// <param name="listAngle">各螺丝角度数组</param>
        /// <param name="listOKorNG">各螺丝OKorNG数组</param>
        /// <param name="ProductOKorNG">产品OKorNG</param>
        public static MESRetrun WorkStationPass(string ProductSN, List<string> listNiuLi, List<string> listAngle, 
            List<bool> listOKorNG, bool ProductOKorNG)
        {
            UnifiedObj.WriteLog(2, "MES写OK 开始\r\n");

            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //ProductSN ="01046174XR72600042";
            //WorkStation = "GYXS-L02-ZXDZYJ01";
            //{ "InterfaceJudgment":true,"ErrInfo":"过站成功",
            //"NJZ_data_list":{ },"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            MESRetrun oMESRetrun = new MESRetrun();
            TightenTheShaft tightenTheShaft = new TightenTheShaft();
            if (listNiuLi.Count != listAngle.Count && listNiuLi.Count != listAngle.Count)
            {
                oMESRetrun.InterfaceJudgment = false;
                oMESRetrun.ErrInfo = "扭力、角度、OKorNG参数数组长度不一致。";
            }
            else
            {
                string strDataPama = "[";
                //扭力
                strDataPama += "[";
                for (int i = 0; i < listNiuLi.Count; i++)
                {
                    strDataPama += "{\"name\":\"fTorques\",\"value\":\"" + listNiuLi[i] + "N.m\"}";
                    if (i < listNiuLi.Count - 1)
                    {
                        strDataPama += ",";
                    }
                }
                strDataPama += "],";
                //角度
                strDataPama += "[";
                for (int i = 0; i < listAngle.Count; i++)
                {
                    strDataPama += "{\"name\":\"fAngles\",\"value\":\"" + listAngle[i] + "°\"}";
                    if (i < listAngle.Count - 1)
                    {
                        strDataPama += ",";
                    }
                }
                strDataPama += "],";
                //单个螺丝结果
                strDataPama += "[";
                for (int i = 0; i < listOKorNG.Count; i++)
                {
                    strDataPama += "{\"name\":\"bOKOrNGs\",\"value\":\"" + listOKorNG[i] + "\"}";
                    if (i < listOKorNG.Count - 1)
                    {
                        strDataPama += ",";
                    }
                }
                strDataPama += "],";
                //产品结果
                strDataPama += "{\"name\":\"bTotalRes\",\"value\":\"" + ProductOKorNG + "\"}]";
                DateTime dtFrom = DateTime.Now;
                try
                {
                    string strReturn = tightenTheShaft.NJZ_MOVE(strMESPama, ProductSN, WorkStation,
                         strDataPama, "DriveScrew", "", "1");
                    UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 打螺丝PASS SN:" + ProductSN + 
                        " 工站信息: " + WorkStation + "\r\n参数:\r\n" + strDataPama + "\r\n返回:\r\n" + strReturn + "\r\n");

                    if (strReturn.Contains("连接服务器异常"))
                    {
                        oMESRetrun = JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                    }
                    else
                    {
                        oMESRetrun = JsonHelper.JsonToObject<MESRetrun>(strReturn);
                    }
                }
                catch (Exception ex)
                {
                    UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 打螺丝PASS SN:" + ProductSN + 
                        " 工站信息: " + WorkStation + "\r\n参数:\r\n" + strDataPama + "\r\n异常:\r\n" + ex.Message + "\r\n");
                    if (ex.Message.Contains("无法连接到远程服务器") || ex.Message.Contains("连接服务器异常"))
                    {
                        return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                    }
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常:" + ex.Message + "\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
            }
            return oMESRetrun;
        }

        /// <summary>
        /// 5. 打螺丝NG 
        /// </summary>
        /// <param name="ProductSN">产品SN</param>
        /// <param name="GongXuName">工序名称</param>
        /// <param name="WorkStation">工站信息 正常作业需要 跳站作业为空</param>
        /// <param name="NGReason">NG原因</param>
        /// <param name="listNiuLi">各螺丝扭力数组</param>
        /// <param name="listAngle">各螺丝角度数组</param>
        /// <param name="listOKorNG">各螺丝OKorNG数组</param>
        /// <param name="ProductOKorNG">产品OKorNG</param>
        public static MESRetrun WorkStationNG(string ProductSN, string GongXuName, string WorkStation,
            string NGReason, List<string> listNiuLi, List<string> listAngle, 
            List<bool> listOKorNG, bool ProductOKorNG)
        {
            UnifiedObj.WriteLog(2, "MES写NG 开始\r\n");

            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //ProductSN = "01046174XR72600044";
            //GongXuName = "GYXS-ZXDZYJ01";
            //WorkStation = "GYXS-L02-ZXDZYJ01";
            //{"InterfaceJudgment":false,"ErrInfo":"产品Rework过站失败：Rework Reason \"返工原因\" 找不到",
            //"NJZ_data_list":{},"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            //ProductSN = "01046174XR72600044";
            //GongXuName = "GYXS-NLS01";//读SN当前工序信息 可得到工序名称
            //WorkStation = "GYXS-L02-ZXDZYJ01";
            //{ "InterfaceJudgment":true,"ErrInfo":"产品Rework过站成功",
            //"NJZ_data_list":{ },"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            MESRetrun oMESRetrun = new MESRetrun();
            TightenTheShaft tightenTheShaft = new TightenTheShaft();
            if (listNiuLi != null && listNiuLi.Count != listAngle.Count && listNiuLi.Count != listAngle.Count)
            {
                oMESRetrun.InterfaceJudgment = false;
                oMESRetrun.ErrInfo = "扭力、角度、OKorNG参数数组长度不一致。";
            }
            else
            {
                if (listNiuLi == null || listNiuLi.Count < 1)
                {
                    listNiuLi = new List<string>();
                    listNiuLi.Add("0");
                }
                if (listAngle == null || listAngle.Count < 1)
                {
                    listAngle = new List<string>();
                    listAngle.Add("0");
                }
                if (listOKorNG == null || listOKorNG.Count < 1)
                {
                    listOKorNG = new List<bool>();
                    listOKorNG.Add(false);
                }

                string strDataPama = "[";
                //扭力
                strDataPama += "[";
                for (int i = 0; i < listNiuLi.Count; i++)
                {
                    strDataPama += "{\"name\":\"fTorques\",\"value\":\"" + listNiuLi[i].Replace("位置超限", "0") + "N.m\"}";
                    if (i < listNiuLi.Count - 1)
                    {
                        strDataPama += ",";
                    }
                }
                strDataPama += "],";
                //角度
                strDataPama += "[";
                for (int i = 0; i < listAngle.Count; i++)
                {
                    strDataPama += "{\"name\":\"fAngles\",\"value\":\"" + listAngle[i].Replace("位置超限", "0") + "°\"}";
                    if (i < listAngle.Count - 1)
                    {
                        strDataPama += ",";
                    }
                }
                
                strDataPama += "],";
                //单个螺丝结果
                strDataPama += "[";
                for (int i = 0; i < listOKorNG.Count; i++)
                {
                    strDataPama += "{\"name\":\"bOKOrNGs\",\"value\":\"" + listOKorNG[i] + "\"}";
                    if (i < listOKorNG.Count - 1)
                    {
                        strDataPama += ",";
                    }
                }
                
                strDataPama += "],";
                //产品结果
                strDataPama += "{\"name\":\"bTotalRes\",\"value\":\"" + ProductOKorNG + "\"}]";

                DateTime dtFrom = DateTime.Now;
                try
                {
                    string strReturn = tightenTheShaft.NJZ_Rework(strMESPama, ProductSN, WorkStation,
                        NGReason, "数采不良", GongXuName, "", "", strDataPama, "DriveScrew");
                    UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 打螺丝NG SN:" + ProductSN + 
                        " 工站信息: " + WorkStation + " 工序名称:" + GongXuName + " " + NGReason + "\r\n参数:\r\n" +
                        strDataPama + "\r\n返回:\r\n" + strReturn + "\r\n");

                    if (strReturn.Contains("连接服务器异常"))
                    {
                        oMESRetrun = JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                    }
                    else
                    {
                        oMESRetrun = JsonHelper.JsonToObject<MESRetrun>(strReturn);
                    }
                }
                catch (Exception ex)
                {
                    UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 打螺丝NG SN:" + ProductSN + 
                        " 工站信息: " + WorkStation + " 工序名称:" + GongXuName + " " + NGReason + "\r\n参数:\r\n" +
                        strDataPama + "\r\n异常:\r\n" + ex.Message + "\r\n");

                    if (ex.Message.Contains("无法连接到远程服务器") || ex.Message.Contains("连接服务器异常"))
                    {
                        return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                    }
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常:" + ex.Message + "\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
            }
            return oMESRetrun;
        }

        /// <summary>
        /// 6. 根据产品SN 工站信息 查询产品是否应送检或停止作业
        /// </summary>
        /// <param name="ProductSN">产品SN</param>
        public static MESRetrun CheckProductIsReWork(string ProductSN)
        {
            UnifiedObj.WriteLog(2, "MES校验SN是否送检或停止作业 开始\r\n");

            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //ProductSN = "0104628M7RA1200697",
            //WorkStation = "GYXS-DGL17-NLS01";
            //{ "InterfaceJudgment":true,"ErrInfo":"GYPDX-ZB","NJZ_data_list":{ },"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            DateTime dtFrom = DateTime.Now;
            try
            {
                TightenTheShaft tightenTheShaft = new TightenTheShaft();
                string strReturn = tightenTheShaft.NJZ_PreviousProcess(strMESPama, ProductSN, WorkStation);
                UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 查询产品是否应送检或停止作业 SN:" + ProductSN + 
                    " 工站信息：" + WorkStation + "\r\n返回:\r\n" + strReturn + "\r\n");

                if (strReturn.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                else
                {
                    return JsonHelper.JsonToObject<MESRetrun>(strReturn);
                }
            }
            catch (Exception ex)
            {
                UnifiedObj.WriteLog(2, "耗时 " + (DateTime.Now - dtFrom).TotalMilliseconds + " 判定产品是否NG送检 SN:" + ProductSN +
                    " 工站信息：" + WorkStation + "\r\n异常:\r\n" + ex.Message + "\r\n");

                if (ex.Message.Contains("无法连接到远程服务器") || ex.Message.Contains("连接服务器异常"))
                {
                    return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
                }
                return JsonHelper.JsonToObject<MESRetrun>("{\"InterfaceJudgment\":false,\"ErrInfo\":\"MES网络或服务异常:" + ex.Message + "\",\"NJZ_data_list\":{ },\"NJZ_DataTable\":[],\"NJZ_DatalistObject\":[]}");
            }
        }

        #region 暂未用到

        //Liju.Common.MES.GetDeviceFile( "A966A01BZDH4260001");
        //Liju.Common.MES.GetToolDeviceList("A96020004938000018063");
        //Liju.Common.MES.GetDianJianDeviceList( "A966A01BZDH4260001");
        //Liju.Common.MES.UpdateDianJianData("A966A01BZDH4260001", "");
        //Liju.Common.MES.CheckToolDevice("A966A01BZDH4260001", "");

        /// <summary>
        /// 6. MES 设备保养规程文件 操作流程
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        public static void GetDeviceFile(string DeviceSN)
        {
            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //"A966A01BZDH4260001"
            //{ "InterfaceJudgment":false,"ErrInfo":"暂无记录。","NJZ_data_list":{ },"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            TightenTheShaft tightenTheShaft = new TightenTheShaft();
            string strReturn = tightenTheShaft.NJZ_DeviceFiles(strMESPama, DeviceSN);
        }

        ///// <summary>
        ///// 11. MES 加载工器具列表
        ///// </summary>
        ///// <param name="userName"></param>
        ///// <param name="password"></param>
        //public static void GetToolDeviceList( string DeviceSN)
        //{
        //    //正式环境 参数及返回值
        //    string strMESPama = "{" +
        //                    "\"IP\":\"" + MesIP + "\"," +
        //                    "\"Port\":" + MesPort + "," +
        //                    "\"UserName\":\"" + UserName + "\"," +
        //                    "\"PW\":\"" + UsernPW + "\"" +
        //                "}";
        //    //"12646753-00", "A/3", "GYXS-ZXDZYJ02"
        //    //{"InterfaceJudgment":true,"ErrInfo":"获取工器具清单成功。判断为不需要校验。",
        //    //"NJZ_data_list":{},"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

        //    TightenTheShaft tightenTheShaft = new TightenTheShaft();
        //    string strReturn = tightenTheShaft.NJZ_ToolLoading(strMESPama, "12646753-00", "A/3", "GYXS-ZXDZYJ02");
        //}

        /// <summary>
        /// 12. MES 加载工器具点检列表
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        public static void GetDianJianDeviceList(string DeviceSN)
        {
            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //"12646753-00", "A/3", "GYXS-ZXDZYJ02"
            //{"InterfaceJudgment":true,"ErrInfo":"不需要点检。",
            //"NJZ_data_list":{},"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            TightenTheShaft tightenTheShaft = new TightenTheShaft();
            string strReturn = tightenTheShaft.NJZ_ToolSpotCheckVerification(strMESPama, "A966A01BZDH4260001");
        }

        /// <summary>
        /// 13. MES 上传工器具点检数据
        /// </summary>
        /// <param name="DeviceSN">工器具SN</param>
        /// <param name="strJson">要上传的数据</param>
        public static void UpdateDianJianData(string DeviceSN, string strJson)
        {
            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //"12646753-00", "A/3", "GYXS-ZXDZYJ02"
            //{"InterfaceJudgment":true,"ErrInfo":"不需要点检。",
            //"NJZ_data_list":{},"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            TightenTheShaft tightenTheShaft = new TightenTheShaft();
            string strReturn = tightenTheShaft.NJZ_ToolSpotCheckUpload(strMESPama, "A966A01BZDH4260001", UserGongHao, strJson);
        }

        /// <summary>
        /// 14. MES 工器具条码校验
        /// </summary>
        /// <param name="DeviceSN">工器具SN</param>
        /// <param name="strJson">要上传的数据</param>
        public static void CheckToolDevice(string DeviceSN, string strJson)
        {
            //正式环境 参数及返回值
            string strMESPama = "{" +
                            "\"IP\":\"" + MesIP + "\"," +
                            "\"Port\":" + MesPort + "," +
                            "\"UserName\":\"" + UserGongHao + "\"," +
                            "\"PW\":\"" + UsernPW + "\"" +
                        "}";
            //"12646753-00", "A/3", "GYXS-ZXDZYJ02"
            //{"InterfaceJudgment":false,"ErrInfo":"工器具条码校验失败，错误信息为：无效的工器具条码，请重新扫描!",
            //"NJZ_data_list":{},"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

            TightenTheShaft tightenTheShaft = new TightenTheShaft();
            string strReturn = tightenTheShaft.NJZ_ToolVerification(strMESPama, "A966A01BZDH4260001",
                "HA-2105100 导通工装", "GYXS-ZXDZYJ02:A");
        }
        #endregion

        /// <summary>
        /// 7-1. 异常报修-加载工器具列表
        /// </summary>
        /// <param name="strToolDeviceSN">工具条码</param>
        public static string GetToolDeviceList(string strToolDeviceSN)
        {
            //strToolDeviceSN ="A966A01BZDH4260001";
            //{"ResponseCode":0,"ResponseMsg":"暂无记录。"}

            //strToolDeviceSN ="A96020004938000018063";
            //{"ResponseCode":1,"ResponseMsg":[{"ResourceCode":"A96020004938000018063","ResourceName":"配电箱手持拧紧轴系统","ResourceType":"资源"}]}

            //strToolDeviceSN ="A966GJ2561";
            //{ "ResponseCode":1,"ResponseMsg":[{"ResourceCode":"A966GJ2561","ResourceName":"HCEF-2105300B 屏蔽环定位套筒","ResourceType":"工装"}]}

            //ToolReworkService toolReworkService = new ToolReworkService();
            //return toolReworkService.GetResourceList(strToolDeviceSN);

            #region 测试数据

            JObject test = new JObject();

            if (strToolDeviceSN == "123456")
            {

                JArray array = new JArray
                {
                    new JObject()
                    {
                        ["ResourceCode"] = "A96020004938000018063",
                        ["ResourceName"]="配电箱手持拧紧轴系统",
                        ["ResourceType"]="资源"
                    }
                };
                test["ResponseCode"] = 1;
                test["ResponseMsg"] = array;

            }
            else if (strToolDeviceSN == "a123456")
            {

                JArray array1 = new JArray
                {
                    new JObject()
                    {
                        ["ResourceCode"] = "A966GJ2561",
                        ["ResourceName"]="HCEF-2105300B 屏蔽环定位套筒",
                        ["ResourceType"]="工装"
                    }
                };
                test["ResponseCode"] = 1;
                test["ResponseMsg"] = array1;


            }
            else
            {
                test["ResponseCode"] = 0;
                test["ResponseMsg"] = "暂无记录。";
            }

            return test.ToString();

            #endregion
        }

        /// <summary>
        /// 7-2. 异常报修-获取全部异常大类信息列表
        /// </summary>
        public static string GetExceptionMenu1()
        {
            //{"ResponseCode":1,"ResponseMsg":[
            //{ "BYD_ComExceptionGroupName":"设备异常"},
            //{ "BYD_ComExceptionGroupName":"物料异常"},
            //{ "BYD_ComExceptionGroupName":"现场管理异常"},
            //{ "BYD_ComExceptionGroupName":"工装类"},
            //{ "BYD_ComExceptionGroupName":"排线调模"},
            //{ "BYD_ComExceptionGroupName":"模具类"},
            //{ "BYD_ComExceptionGroupName":"适配件"},
            //{ "BYD_ComExceptionGroupName":"电脑"},
            //{ "BYD_ComExceptionGroupName":"网络"},
            //{ "BYD_ComExceptionGroupName":"MES"},
            //{ "BYD_ComExceptionGroupName":"型枪类"},
            //{ "BYD_ComExceptionGroupName":"五工位设备类"},
            //{ "BYD_ComExceptionGroupName":"拧紧轴"},
            //{ "BYD_ComExceptionGroupName":"激光设备类"},
            //{ "BYD_ComExceptionGroupName":"焊接机异常"}]}
            //ToolReworkService toolReworkService = new ToolReworkService();
            //return toolReworkService.GetComExceptionGroupList();

            #region 测试数据
            JObject test = new JObject
            {
                ["ResponseCode"] = 1
            };

            JArray array = new JArray
            {
                new JObject()
                {
                    ["BYD_ComExceptionGroupName"] = "设备异常"
                },
                new JObject()
                {
                    ["BYD_ComExceptionGroupName"] = "物料异常"
                }
            };

            test["ResponseMsg"] = array;

            return test.ToString();
            #endregion
        }

        /// <summary>
        /// 7-3. MES 异常报修-根据异常大类获取常规异常信息列表
        /// </summary>
        /// <param name="ExceptionMenu1">异常大类名称</param>
        public static string GetExceptionMenu2(string ExceptionMenu1)
        {
            //ExceptionMenu1 = "设备异常";
            //{ "ResponseCode":1,"ResponseMsg":[
            //{ "BYD_CommonExceptionName":"SBGZ01","Description":"设备无法压接"},
            //{ "BYD_CommonExceptionName":"NJZ0001","Description":"扭力校正调试扭力"},
            //{ "BYD_CommonExceptionName":"SBGZ02","Description":"设备频繁NG"}]}

            //ExceptionMenu1 = "工装类";
            //{"ResponseCode":1,"ResponseMsg":[
            //{ "BYD_CommonExceptionName":"GZ01","Description":"结构损坏"},
            //{ "BYD_CommonExceptionName":"GZ04","Description":"工装漏气"},
            //{ "BYD_CommonExceptionName":"GZ02","Description":"检测误报"}]}

            //ToolReworkService toolReworkService = new ToolReworkService();
            //return toolReworkService.GetCommonExceptionList(ExceptionMenu1);

            #region 测试数据
            JObject test = new JObject
            {
                ["ResponseCode"] = 1
            };

            if (ExceptionMenu1 == "设备异常")
            {

                JArray array = new JArray
                {
                    new JObject()
                    {
                        ["BYD_ComExceptionGroupName"] = "SBGZ01",
                        ["Description"]="设备无法压接"
                    },
                    new JObject()
                    {
                        ["BYD_ComExceptionGroupName"] = "NJZ0001",
                        ["Description"]="扭力校正调试扭力"
                    },
                    new JObject()
                    {
                        ["BYD_ComExceptionGroupName"] = "SBGZ02",
                        ["Description"]="设备频繁NG"
                    }
                };
                test["ResponseMsg"] = array;

            }
            else
            {

                JArray array1 = new JArray
                {
                    new JObject()
                    {
                        ["BYD_ComExceptionGroupName"] = "GZ01",
                        ["Description"]="结构损坏"
                    },
                    new JObject()
                    {
                        ["BYD_ComExceptionGroupName"] = "GZ04",
                        ["Description"]="工装漏气"
                    },
                    new JObject()
                    {
                        ["BYD_ComExceptionGroupName"] = "GZ02",
                        ["Description"]="检测误报"
                    }
                };
                test["ResponseMsg"] = array1;


            }

            return test.ToString();
            #endregion
        }

        /// <summary>
        /// 7-4. 异常报修-保存工器具报修信息
        /// </summary>
        /// <param name="ToolDeviceSN">工器具条码</param>
        /// <param name="ToolDeviceName">工器具名称</param>
        /// <param name="Workstation">工位</param>
        /// <param name="Line">产线</param>
        /// <param name="ExceptionMenu1">异常大类名称</param>
        /// <param name="ExceptionMenu2">异常二级类名称</param>
        public static string SaveExceptionInfo(string ToolDeviceSN, string ToolDeviceName, string ExceptionMenu1, string ExceptionMenu2)
        {
            //ToolDeviceSN = "A966GJ2561";
            //ToolDeviceName = "HCEF-2105300B 屏蔽环定位套筒";
            //Workstation = "GYXS-DGL17-NLS01";
            //Line = "D01-GYXS-L17";
            //ExceptionMenu1 = "工装类";
            //ExceptionMenu2 = "检测误报";
            //{"ResponseCode":1,"ResponseMsg":"A966GJ2561报修成功！"}

            //ToolReworkService toolReworkService = new ToolReworkService();
            //return toolReworkService.ToolRework(ToolDeviceSN, ToolDeviceName, WorkStation,
            //   Line, ExceptionMenu1, ExceptionMenu2, UserGongHao, UsernPW);

            #region 测试数据
            JObject test = new JObject
            {
                ["ResponseCode"] = 1,
                ["ResponseMsg"] = "A966GJ2561报修成功！"
            };

            return test.ToString();
            #endregion


        }

        /// <summary>
        /// 7-5. 异常报修-已维修待用户确认记录
        /// </summary>
        public static string GetNeedConfirmRepairsList()
        {
            //{ "ResponseCode":0,"ResponseMsg":"暂无记录。"}

            //{"ResponseCode":1,"ResponseMsg":[{"SequenceNo":1,"ResourceCode":"A966ZMJ00555","ResourceName":"六方油压机刀模-8mm","CommonException":"刀模异常","RepairPerson":"段华伟","RepairDate":"2024-07-29 14:49:37","WorkStation":"工具室","ResourceType":"六方油压机刀模-8mm","BYD_ToolReworkID":"480153800003c173","BYD_ToolRepairID":"48015d800000ec45"}]}

            //ToolReworkService toolReworkService = new ToolReworkService();
            //return toolReworkService.GetUserConfirmResourceRepairsList();

            #region 测试数据

            JObject test = new JObject()
            {
                ["ResponseCode"] = 1
            };

            JArray array = new JArray()
            {
                new JObject() {
                    ["SequenceNo"]= 1,
                    ["ResourceCode"]= "A966ZMJ00555",
                    ["ResourceName"]= "六方油压机刀模-8mm",
                    ["CommonException"]= "刀模异常",
                    ["RepairPerson"]= "段华伟",
                    ["RepairDate"]= "2024-07-29 14:49:37",
                    ["WorkStation"]= "工具室",
                    ["ResourceType"]= "六方油压机刀模-8mm",
                    ["BYD_ToolReworkID"]= "480153800003c173",
                    ["BYD_ToolRepairID"]= "48015d800000ec45"
                }
            };

            test["ResponseMsg"] = array;
            return test.ToString();

            #endregion
        }

        /// <summary>
        /// 7-6. 异常报修-用户确认维修信息
        /// </summary>
        /// <param name="ExceptionMenu1">异常大类名称</param>
        public static string UserConfirmRepair(UserConfirmInputInfo[] arrUserConfirmInputInfo)
        {
            ToolReworkService toolReworkService = new ToolReworkService();
            return toolReworkService.UserConfirmRepair(UserGongHao, arrUserConfirmInputInfo, UserGongHao, UsernPW);
        }

        /// <summary>
        /// 8-1. 产品送检-获取全部产品信息
        /// </summary>
        /// <param name="ProductSN">待送检产品SN</param>
        public static string GetSongJianProductInfo(string ProductSN)
        {
            //ProductSN = "01046174XR72600042";
            //WorkStation = "GYXS-DGL17-NLS01";
            //{"ResponseCode":0,"ResponseMsg":"产品条码工位[GYXS-DGL17-ZB]与当前工位[GYXS-DGL17-NLS01]不符，不允许送检！"}

            //ProductSN = "01046174XR72600042";
            //WorkStation = "GYXS-DGL17-ZB";
            //{"ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003118247347","MfgOrderStartDate":"2024-07-26 08:00:00","MfgOrderQty":"112","ProductCode":"13350560-00","ProductName":"HCHN-2106010集成式充电插座总成","CurStation":"GYXS-DGL17-ZB","Reworkflow":"13350560-00","ReworkStep":"GYXS-ZB","WorkflowStepId":"00074e80000274c5"}]}

            //010463QJAR80100043
            //{ "ResponseCode":0,"ResponseMsg":"产品条码工位[GYXS-DGL16-TMDY]与当前工位[GYXS-DGL17-NLS01]不符，不允许送检！"}

            //010463XEVR80300029
            //{"ResponseCode":0,"ResponseMsg":"产品条码工位[GYPDX-DGL31-JYNYJC]与当前工位[GYXS-DGL17-NLS01]不符，不允许送检！"}
            //{ "ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003903298525","MfgOrderStartDate":"2024-08-03 08:00:00","MfgOrderQty":"77","ProductCode":"16984994-00","ProductName":"HCEEQ-2123300_高压配电总成","CurStation":"GYPDX-DGL31-JYNYJC","Reworkflow":"16984994-00","ReworkStep":"GYPDX-JYNYJC","WorkflowStepId":"00074e80000289c8"}]}

            //0104616HRR80300258
            //GYXS-DGL21-ZHJCT
            //{"ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003118357567","MfgOrderStartDate":"2024-08-03 08:00:00","MfgOrderQty":"1000","ProductCode":"13285828-00","ProductName":"HCE-2105410A_空调PTC线束子总成","CurStation":"GYXS-DGL21-ZHJCT","Reworkflow":"13285828-00-Flow","ReworkStep":"GYXS-ZHJCT","WorkflowStepId":"00074e8000025068"}]}

            ProdReworkService.ProdReworkService prodReworkService = new ProdReworkService.ProdReworkService();
            return prodReworkService.GetProductInfoList(ProductSN, WorkStation);

            #region 测试数据

            //JObject test = new JObject();
            //if (ProductSN == "1")
            //{
            //    test["ResponseCode"] = 0;
            //    test["ResponseMsg"] = "产品条码工位[GYXS-DGL17-ZB]与当前工位[GYXS-DGL17-NLS01]不符，不允许送检！";
            //}
            //else
            //{

            //    JArray array = new JArray()
            //    {
            //        new JObject() {
            //            ["MfgOrderName"]= "003903298525",
            //            ["MfgOrderStartDate"]= "2024-08-03 08:00:00",
            //            ["MfgOrderQty"]= "77",
            //            ["ProductCode"]= "16984994-00",
            //            ["ProductName"]= "HCEEQ-2123300_高压配电总成",
            //            ["CurStation"]= "GYPDX-DGL31-JYNYJC",
            //            ["Reworkflow"]= "16984994-00",
            //            ["ReworkStep"]= "GYPDX-JYNYJC",
            //            ["WorkflowStepId"]= "00074e80000289c8"
            //        }
            //    };
            //        test["ResponseCode"] = 1;
            //        test["ResponseMsg"] = array;
            //}

            //return test.ToString();

            #endregion

        }

        /// <summary>
        /// 8-2. 产品送检-根据送检工序ID获取送检原因信息 下拉列表用
        /// </summary>
        /// <param name="WorkflowStepID">送检工序ID 由8.1接口返回参数</param>
        public static string GetSongJianReasonList(string WorkflowStepID)
        {
            //WorkflowStepID="GYXS-NLS01";
            //{"ResponseCode":0,"ResponseMsg":"没有找到对应的送检原因。"}

            //WorkflowStepID = "00074e80000274c5";
            //{"ResponseCode":1,"ResponseMsg":[{"ReworkReasonName":"压接不良"},{"ReworkReasonName":"外观不良"},
            //{ "ReworkReasonName":"插拔力大"},{"ReworkReasonName":"插拔力小"},{"ReworkReasonName":"插板力不合格，小于35N"},
            //{ "ReworkReasonName":"数采不良"},{"ReworkReasonName":"电子锁不导通"},{"ReworkReasonName":"电子锁伸出尺寸小"},
            //{ "ReworkReasonName":"电子锁伸出长度不够"},{"ReworkReasonName":"装配不良"}]}

            //00074e80000289c8
            //{"ResponseCode":1,"ResponseMsg":[{"ReworkReasonName":"壳体漏电流大于3mA"},
            //{ "ReworkReasonName":"壳体绝缘电阻≤50MΩ"},{"ReworkReasonName":"数采不良"},
            //{ "ReworkReasonName":"标贴粘贴位置不正确"},{"ReworkReasonName":"漏电流大于3mA"}]}

            //00074e8000025068
            //{"ResponseCode":1,"ResponseMsg":[{"ReworkReasonName":"压接不良"},{"ReworkReasonName":"外观不良"},
            //{ "ReworkReasonName":"插拔力大"},{"ReworkReasonName":"插拔力小"},
            //{ "ReworkReasonName":"插板力不合格，小于35N"},{"ReworkReasonName":"数采不良"},
            //{ "ReworkReasonName":"电子锁不导通"},{"ReworkReasonName":"电子锁伸出尺寸小"},
            //{ "ReworkReasonName":"电子锁伸出长度不够"},{"ReworkReasonName":"装配不良"}]}

            //ProdReworkService.ProdReworkService prodReworkService = new ProdReworkService.ProdReworkService();
            //return prodReworkService.GetReworkReasonList(WorkflowStepID);

            #region 测试数据

            JObject test = new JObject()
            {
                ["ResponseCode"]=1
            };

            JArray array = new JArray()
            {
                new JObject() 
                {
                    ["ReworkReasonName"]="压接不良"
                },
                new JObject()
                {
                    ["ReworkReasonName"]="外观不良"
                },
                new JObject()
                {
                    ["ReworkReasonName"]="插拔力大"
                },
                new JObject()
                {
                    ["ReworkReasonName"]="插拔力小"
                },
                new JObject()
                {
                    ["ReworkReasonName"]="数采不良"
                }
            };
            test["ResponseMsg"]=array;

            return test.ToString();
            #endregion
        }

        /// <summary>
        /// 8-3. 产品送检-保存送检信息
        /// </summary>
        /// <param name="PiCiCode">送检批次条码</param>
        /// <param name="SongJianReason">送检原因</param>
        /// <param name="SongJianMemo">送检备注</param>
        /// <param name="SongJianGongXuId">送检工序ID</param>
        /// <param name="SongJianGongXu">送检工序</param>
        /// <param name="ProductSN">待送检产品SN</param>
        public static string SaveSongJianInfo(string PiCiCode, string SongJianReason, string SongJianMemo,
            string SongJianGongXuId, string SongJianGongXu, string ProductSN)
        {
            //2302sw
            //数采不良
            //产品NG原因：测试，实际不送检
            //00074e8000025068
            //GYXS-ZHJCT
            //0104616HRR80300258
            //{"ResponseCode":1,"ResponseMsg":"2302sw送检成功！"}

            //ProdReworkService.ProdReworkService prodReworkService = new ProdReworkService.ProdReworkService();
            //return prodReworkService.ProdRework(PiCiCode, SongJianReason, SongJianMemo,
            //    SongJianGongXuId, SongJianGongXu, Line, ProductSN, UserGongHao, UsernPW);

            #region 测试数据
            JObject test = new JObject()
            {
                ["ResponseCode"] = 1,
                ["ResponseMsg"] = "2302sw送检成功"
            };
            return test.ToString();

            #endregion
        }

        /// <summary>
        /// 10-1. 文件查看-获取线体工单信息
        /// </summary>
        /// <param name="strDateFrom">开始时间</param>
        /// <param name="strDateTo">结束时间</param>
        public static string GetLineOrderList(string strDateFrom, string strDateTo)
        {
            //strDateFrom = "2024-07-30";
            //strDateTo = "2024-07-31";
            //Line = "D01-GYXS-L17";
            //{"ResponseCode":0,"ResponseMsg":"暂无记录。"}

            //{ "ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003118295237"},{"MfgOrderName":"003903278097"}]}

            //ESOPService.ESOPService eSOPService = new ESOPService.ESOPService();
            //return eSOPService.GetMfgOrderList(strDateFrom, strDateTo, Line);

            #region 测试数据
            JObject test = new JObject()
            {
                ["ResponseCode"] = 1
                
            };

            JArray array = new JArray()
            {
                new JObject {
                    ["MfgOrderName"]="003118295237"
                },
                new JObject {
                    ["MfgOrderName"]="003903278097"
                }
            };
            test["ResponseMsg"] = array;

            return test.ToString();

            #endregion

        }

        /// <summary>
        /// 10-2. 文件查看-根据工单获取产品SOP文件信息
        /// </summary>
        /// <param name="strOrder">工单号</param>
        public static string GetSOPByOrder(string strOrder)
        {
            //strOrder = "003118295237";
            //{"ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003118295237","MfgLineName":"D01-GYXS-L17","ProductCode":"15910717-00","ProductName":"HCEK-2106100H交直流一体充电车辆插座分总成","SopFileNumber":"","SopFileVersion":"","CraftcardName":"","CraftcardRevision":"","Qty":null,"QtyStarted":null}]}

            //{ "ResponseCode":0,"ResponseMsg":"暂无记录。"}

            //ESOPService.ESOPService eSOPService = new ESOPService.ESOPService();
            //return eSOPService.GetProductList(strOrder);

            #region 测试数据
            JObject test = new JObject()
            {
                ["ResponseCode"] = 1

            };

            JArray array = new JArray()
            {
                new JObject {
                    ["MfgOrderName"]="003118295237",
                    ["MfgLineName"]="D01-GYXS-L17",
                    ["ProductCode"]="15910717-00",
                    ["ProductName"]="HCEK-2106100H交直流一体充电车辆插座分总成",
                    ["SopFileNumber"]="SOP文件",
                    ["SopFileVersion"]="V1.1.1.1"
                }
            };
            test["ResponseMsg"] = array;

            return test.ToString();

            #endregion

        }

        /// <summary>
        /// 10-3. 文件查看-根据设备编码获取ESOP文件信息
        /// </summary>
        /// <param name="strToolDeviceCode">工器具条码</param>
        public static string GetToolDeviceESOP(string strToolDeviceCode)
        {
            //strToolDeviceCode = "A96020004938000018063";
            //{ "ResponseCode":1,"ResponseMsg":[{"ResourceCode":"A96020004938000018063","ResourceName":"配电箱手持拧紧轴系统","ResourceType":"资源","MaintenanceDocument":"BYD-GC-C03F03-10608","OperationDocument":"BYD-SP-C03F03-10410"}]}

            //strToolDeviceCode = "A9602000725420230220";
            //{"ResponseCode":1,"ResponseMsg":[{"ResourceCode":"A9602000725420230220","ResourceName":"","ResourceType":"资源","MaintenanceDocument":"","OperationDocument":""}]}

            //strToolDeviceCode = "A9602000658820211220";
            //{"ResponseCode":1,"ResponseMsg":[{"ResourceCode":"A9602000658820211220","ResourceName":"重型端子压接机","ResourceType":"资源","MaintenanceDocument":"BYD-GC-C03F03-10727","OperationDocument":"BYD-SP-C03F03-10557"}]}
            //http://div14-ts.byd.com.cn/Front/SubFilesShowForm.aspx?FileNumber=BYD-GC-C03F03-10727&Version=&Old=643C01EB-F8F2-47D4-8174-418D9F316019
            //http://div14-ts.byd.com.cn/Front/SubFilesShowForm.aspx?FileNumber=BYD-SP-C03F03-10557&Version=&Old=643C01EB-F8F2-47D4-8174-418D9F316019

            //{"ResponseCode":0,"ResponseMsg":"暂无记录。"}

            //ESOPService.ESOPService eSOPService = new ESOPService.ESOPService();
            //return eSOPService.GetResourceList(strToolDeviceCode);

            #region 测试数据
            JObject test = new JObject()
            {
                ["ResponseCode"] = 1

            };

            JArray array = new JArray()
            {
                new JObject {
                    ["ResourceCode"]="A9602000658820211220",
                    ["ResourceName"]="重型端子压接机",
                    ["ResourceType"]="资源",
                    ["MaintenanceDocument"]="BYD-GC-C03F03-10727",
                    ["OperationDocument"]="BYD-SP-C03F03-10557"
                }
            };
            test["ResponseMsg"] = array;

            return test.ToString();

            #endregion
        }

        /// <summary>
        /// 写文件锁
        /// </summary>
        static object writeFileLocker = new object();

        private static void WriteFile(string path, string content)
        {
            lock (writeFileLocker) //写文件时要加锁
            {
                if (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path)) == false)
                    System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read))
                {
                    if (fs.CanWrite)
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.WriteLine(content);
                            sw.Flush();
                        }
                    }
                    fs.Close();
                }
            }

        }


        /// <summary>
        /// post请求方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="json">json参数</param>
        /// <returns></returns>
        public static string PostHtmlByJson(string url, string json)
        {
            string result = string.Empty;
            //object obj;
            try
            {
                var request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
                if (request != null)
                {
                    request.ContentType = "application/json";
                    request.Method = "post";

                    //request.CookieContainer = _cookie;
                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        streamWriter.Write(json);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                    System.Net.HttpWebResponse response;
                    try
                    {
                        response = (System.Net.HttpWebResponse)request.GetResponse();

                    }
                    catch (System.Net.WebException e)
                    {
                        response = e.Response as System.Net.HttpWebResponse;
                    }
                    if (response != null)
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
                else result = "error:无效的URL！";
            }
            catch (UriFormatException ex)
            {
                result = "error:" + ex.Message;
            }
            catch (System.Net.WebException ex)
            {
                result = "error:" + ex.Message;
            }
            return result;
        }


    }

    /// <summary>
    /// MES接口返回数据
    /// </summary>
    [DataContract]
    public class MESRetrun
    {
        // { "InterfaceJudgment":true,"ErrInfo":"登录成功","NJZ_data_list":{ },
        // "NJZ_DataTable":[],"NJZ_DatalistObject":[]}

        // {"InterfaceJudgment":true,"ErrInfo":"获取设备信息成功：",
        // "NJZ_data_list":{"DevType":{"Name":"设备信息","Value":"电动拧紧轴03"}},
        // "NJZ_DataTable":[],"NJZ_DatalistObject":[]}

        //{ "InterfaceJudgment":true,"ErrInfo":"资源校验通过:",
        //"NJZ_data_list":{ "OrderNumber":{ "Name":"订单号","Value":"003118247347"},"ProductionLine":{ "Name":"生产线","Value":"D01-GYXS-L17"},
        //"WorkingProcedure":{ "Name":"当前工序","Value":"GYXS-NLS01"},"MaterialNo":{ "Name":"物料号","Value":"13350560-00"},
        //"MaterialRev":{ "Name":"物料版本","Value":"C/3"},"MaterialName":{ "Name":"物料名","Value":"HCHN-2106010集成式充电插座总成"},
        //"Workflow":{ "Name":"工作流","Value":"13350560-00"} },
        //"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

        /// <summary>
        /// 返回结果 
        /// </summary>
        [DataMember]
        public bool InterfaceJudgment;

        /// <summary>
        /// 返回结果信息描述
        /// </summary>
        [DataMember]
        public string ErrInfo;

        [DataMember]
        public MESData NJZ_data_list;

        [DataMember]
        public string NJZ_DataTable;

        [DataMember]
        public string NJZ_DatalistObject;
    }

    /// <summary>
    /// MES接口返回数据 订单 物料 工艺信息
    /// </summary>
    [DataContract]
    public class MESData
    {
        // {"InterfaceJudgment":true,"ErrInfo":"获取设备信息成功：",
        // "NJZ_data_list":{"DevType":{"Name":"设备信息","Value":"电动拧紧轴03"}},
        // "NJZ_DataTable":[],"NJZ_DatalistObject":[]}

        //{ "InterfaceJudgment":true,"ErrInfo":"资源校验通过:",
        //"NJZ_data_list":{ "OrderNumber":{ "Name":"订单号","Value":"003118247347"},"ProductionLine":{ "Name":"生产线","Value":"D01-GYXS-L17"},
        //"WorkingProcedure":{ "Name":"当前工序","Value":"GYXS-NLS01"},"MaterialNo":{ "Name":"物料号","Value":"13350560-00"},
        //"MaterialRev":{ "Name":"物料版本","Value":"C/3"},"MaterialName":{ "Name":"物料名","Value":"HCHN-2106010集成式充电插座总成"},
        //"Workflow":{ "Name":"工作流","Value":"13350560-00"} },
        //"NJZ_DataTable":[],"NJZ_DatalistObject":[]}

        /// <summary>
        /// 设备信息 
        /// </summary>
        [DataMember]
        public Device DevType;

        /// <summary>
        /// 订单号
        /// </summary>
        [DataMember]
        public OrderNumber OrderNumber;

        /// <summary>
        /// 生产线
        /// </summary>
        [DataMember]
        public ProductionLine ProductionLine;

        /// <summary>
        /// 当前工序名称
        /// </summary>
        [DataMember]
        public WorkingProcedure WorkingProcedure;

        /// <summary>
        /// 物料号
        /// </summary>
        [DataMember]
        public MaterialNo MaterialNo;

        /// <summary>
        /// 物料版本
        /// </summary>
        [DataMember]
        public MaterialRev MaterialRev;

        /// <summary>
        /// 物料名
        /// </summary>
        [DataMember]
        public MaterialName MaterialName;

        /// <summary>
        /// 工作流
        /// </summary>
        [DataMember]
        public Workflow Workflow;
    }

    #region  MES接口返回数据 子元素
    /// <summary>
    /// 设备信息
    /// </summary>
    [DataContract]
    public class Device
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }

    /// <summary>
    /// 订单号
    /// </summary>
    [DataContract]
    public class OrderNumber
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }

    /// <summary>
    /// 生产线
    /// </summary>
    [DataContract]
    public class ProductionLine
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }

    /// <summary>
    /// 当前工序
    /// </summary>
    [DataContract]
    public class WorkingProcedure
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }

    /// <summary>
    /// 物料号
    /// </summary>
    [DataContract]
    public class MaterialNo
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }

    /// <summary>
    /// 物料版本
    /// </summary>
    [DataContract]
    public class MaterialRev
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }

    /// <summary>
    /// 物料名
    /// </summary>
    [DataContract]
    public class MaterialName
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }

    /// <summary>
    /// 工作流
    /// </summary>
    [DataContract]
    public class Workflow
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Value;
    }
    #endregion

    /// <summary>
    /// MES接口返回数据
    /// </summary>
    [DataContract]
    public class MESRetrun2
    {
        //{"ResponseCode":1,"ResponseMsg":[{"ResourceCode":"A96020004938000018063","ResourceName":"配电箱手持拧紧轴系统","ResourceType":"资源"}]}
        //{"ResponseCode":1,"ResponseMsg":[{"ResourceCode":"A96020004938000018063","ResourceName":"配电箱手持拧紧轴系统","ResourceType":"资源","MaintenanceDocument":"BYD-GC-C03F03-10608","OperationDocument":"BYD-SP-C03F03-10410"}]}

        //{ "ResponseCode":1,"ResponseMsg":[
        //{ "BYD_ComExceptionGroupName":"设备异常"},
        //{ "BYD_ComExceptionGroupName":"物料异常"},
        //{ "BYD_ComExceptionGroupName":"工装类"},
        //{ "BYD_ComExceptionGroupName":"排线调模"},
        //{ "BYD_ComExceptionGroupName":"模具类"},
        //{ "BYD_ComExceptionGroupName":"网络"},
        //{ "BYD_ComExceptionGroupName":"MES"},
        //{ "BYD_ComExceptionGroupName":"型枪类"},
        //{ "BYD_ComExceptionGroupName":"拧紧轴"}]}

        //{ "ResponseCode":1,"ResponseMsg":[
        //{ "BYD_CommonExceptionName":"SBGZ01","Description":"设备无法压接"},
        //{ "BYD_CommonExceptionName":"NJZ0001","Description":"扭力校正调试扭力"},
        //{ "BYD_CommonExceptionName":"SBGZ02","Description":"设备频繁NG"}]}

        //{"ResponseCode":1,"ResponseMsg":[{"SequenceNo":1,"ResourceCode":"A966ZMJ00555","ResourceName":"六方油压机刀模-8mm","CommonException":"刀模异常","RepairPerson":"段华伟","RepairDate":"2024-07-29 14:49:37","WorkStation":"工具室","ResourceType":"六方油压机刀模-8mm","BYD_ToolReworkID":"480153800003c173","BYD_ToolRepairID":"48015d800000ec45"}]}

        //{"ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003118247347","MfgOrderStartDate":"2024-07-26 08:00:00","MfgOrderQty":"112","ProductCode":"13350560-00","ProductName":"HCHN-2106010集成式充电插座总成","CurStation":"GYXS-DGL17-ZB","Reworkflow":"13350560-00","ReworkStep":"GYXS-ZB","WorkflowStepId":"00074e80000274c5"}]}

        //{"ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003118295237"},{"MfgOrderName":"003903278097"}]}

        //{"ResponseCode":1,"ResponseMsg":[{"MfgOrderName":"003118295237","MfgLineName":"D01-GYXS-L17","ProductCode":"15910717-00","ProductName":"HCEK-2106100H交直流一体充电车辆插座分总成","SopFileNumber":"","SopFileVersion":"","CraftcardName":"","CraftcardRevision":"","Qty":null,"QtyStarted":null}]}


        /// <summary>
        /// 返回结果 0失败 1成功
        /// </summary>
        [DataMember]
        public bool ResponseCode;

        /// <summary>
        /// 返回结果信息描述
        /// </summary>
        [DataMember]
        public string ResponseMsg;

        ///// <summary>
        ///// 返回结果信息描述
        ///// </summary>
        //[DataMember]
        //public ResponseMsg ResponseMsg;

    }

    /// <summary>
    /// MES接口返回数据
    /// </summary>
    [DataContract]
    public class MESRetrun3
    {
        /// <summary>
        /// 返回结果 0失败 1成功
        /// </summary>
        [DataMember]
        public bool ResponseCode;

    }

    ///// <summary>
    ///// MES接口返回数据 异常报修 送检 文件查看信息
    ///// </summary>
    //[DataContract]
    //public class ResponseMsg
    //{
    //    /// <summary>
    //    /// 工器具编码
    //    /// </summary>
    //    [DataMember]
    //    string ResourceCode;

    //    /// <summary>
    //    /// 工器具名称
    //    /// </summary>
    //    [DataMember]
    //    string ResourceName;

    //    /// <summary>
    //    /// 资源类型
    //    /// </summary>
    //    [DataMember]
    //    string ResourceType;

    //    /// <summary>
    //    /// 异常大类名称
    //    /// </summary>
    //    [DataMember]
    //    string BYD_ComExceptionGroupName;

    //    /// <summary>
    //    /// 异常二类名称
    //    /// </summary>
    //    [DataMember]
    //    string BYD_CommonExceptionName;

    //    /// <summary>
    //    /// 异常二类描述
    //    /// </summary>
    //    [DataMember]
    //    string Description;

    //    /// <summary>
    //    /// 序号
    //    /// </summary>
    //    [DataMember]
    //    string SequenceNo;

    //    /// <summary>
    //    /// 异常明细
    //    /// </summary>
    //    [DataMember]
    //    string CommonException;

    //    /// <summary>
    //    /// 维修人员
    //    /// </summary>
    //    [DataMember]
    //    string RepairPerson;

    //    /// <summary>
    //    /// 维修时间
    //    /// </summary>
    //    [DataMember]
    //    string RepairDate;

    //    /// <summary>
    //    /// 工位
    //    /// </summary>
    //    [DataMember]
    //    string WorkStation;

    //    /// <summary>
    //    /// 报修ID
    //    /// </summary>
    //    [DataMember]
    //    string BYD_ToolReworkID;

    //    /// <summary>
    //    /// 维修ID
    //    /// </summary>
    //    [DataMember]
    //    string BYD_ToolRepairID;

    //    /// <summary>
    //    /// 工单号
    //    /// </summary>
    //    [DataMember]
    //    string MfgOrderName;

    //    /// <summary>
    //    /// 工单生产时间
    //    /// </summary>
    //    [DataMember]
    //    string MfgOrderStartDate;

    //    /// <summary>
    //    /// 工单数量
    //    /// </summary>
    //    [DataMember]
    //    string MfgOrderQty;

    //    /// <summary>
    //    /// 产品编码
    //    /// </summary>
    //    [DataMember]
    //    string ProductCode;

    //    /// <summary>
    //    /// 产品名称
    //    /// </summary>
    //    [DataMember]
    //    string ProductName;

    //    /// <summary>
    //    /// 当前工站
    //    /// </summary>
    //    [DataMember]
    //    string CurStation;

    //    /// <summary>
    //    /// 工作流
    //    /// </summary>
    //    [DataMember]
    //    string Reworkflow;

    //    /// <summary>
    //    /// 送检工序
    //    /// </summary>
    //    [DataMember]
    //    string ReworkStep;

    //    /// <summary>
    //    /// 送检工序ID
    //    /// </summary>
    //    [DataMember]
    //    string WorkflowStepId;

    //    /// <summary>
    //    /// 产线编码
    //    /// </summary>
    //    [DataMember]
    //    string MfgLineName;

    //    /// <summary>
    //    /// SOP文件
    //    /// </summary>
    //    [DataMember]
    //    string SopFileNumber;

    //    /// <summary>
    //    /// SOP文件版本
    //    /// </summary>
    //    [DataMember]
    //    string SopFileVersion;

    //    /// <summary>
    //    /// SOP文件信息
    //    /// </summary>
    //    [DataMember]
    //    string CraftcardName;

    //    /// <summary>
    //    /// SOP文件信息
    //    /// </summary>
    //    [DataMember]
    //    string CraftcardRevision;

    //    /// <summary>
    //    /// SOP文件信息
    //    /// </summary>
    //    [DataMember]
    //    string Qty;

    //    /// <summary>
    //    /// SOP文件信息
    //    /// </summary>
    //    [DataMember]
    //    string QtyStarted;

    //    /// <summary>
    //    /// 保养规程
    //    /// </summary>
    //    [DataMember]
    //    string MaintenanceDocument;

    //    /// <summary>
    //    /// 操作规程
    //    /// </summary>
    //    [DataMember]
    //    string OperationDocument;

    //}

}
